Step 1: Import the requests library
In [1]:
import requests
Step 2: Get the file using the URL and assign it to a suitable variable. Thanks to Dr Chuck for the file and his awesome tutorials.
In [2]:
file = requests.get('http://data.pr4e.org/romeo.txt')
Step 3: Process the file according to your needs. Note that the contents of your file are stored in the "text" attribute.
In [3]:
# Put the lines and words in suitably-named lists. Note the use of strip() to remove any pesky extra lines or spaces.
linesList = file.text.strip().split('\n')
wordsList = file.text.strip().split()
# Print the file line-by-line.
for line in linesList:
print (line)
# Print the number of words and lines.
print ()
print ('There are', len(wordsList), 'words and', len(linesList), 'lines in the file.')